﻿using Leeder.Logger;
using LeederGlobal;
using NewAndong.Business;
using NewAndong.Model;
using Sunny.UI;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace NewAndong.Forms
{
    public partial class FrmTransportGroupSet : UIPage
    {
        bool isLoad = true;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public LogOperate LogObj => Log.GetLogger(this.GetType());

        //TransportGroupBLL transportGroupBLL = new TransportGroupBLL();
        DirectQuoteBLL directQuoteBLL = new DirectQuoteBLL();
        UnpackingDirectionBLL unpackingDirectionBLL = new UnpackingDirectionBLL();
        MaterialDirectionBLL materialDirectionBLL = new MaterialDirectionBLL();

        public FrmTransportGroupSet()
        {
            InitializeComponent();
        }

        private void FrmTransportGroupSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            Init_Control();
            RefreshDgv();
        }

        private void Init_Control()
        {
            dgvTransportGroup.AutoGenerateColumns = false;
            dgvTransportGroup.AllowUserToAddRows = false;
            dgvTransportGroup.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvTransportGroup.ReadOnly = true;
            //dgvTransportGroup.AddColumn("序号", "ID", 25);
            dgvTransportGroup.AddColumn("出发链组名", "DirectName", 25);
            //dgvTransportGroup.AddColumn("数据源", "SignalMark", 25);
            dgvTransportGroup.AddColumn("是否启用", "IsUse", 25);
            //dgvTransportGroup.AddColumn("类型", "TypeName", 34);

            uiPanel2.RectColor = Color.FromArgb(235, 243, 255);
        }

        private void RefreshDgv()
        {
            var list = directQuoteBLL.GetModelList();
            var cflUseList = list.Where(w => w.Type == "CFL" && w.IsUse == 1).ToList();
            dgvTransportGroup.DataSource = null;
            dgvTransportGroup.DataSource = cflUseList;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            Edit();
            RefreshDgv();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            Edit(dgvTransportGroup.CurrentRow.DataBoundItem as DirectQuoteModel);
            RefreshDgv();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }

            if (UIMessageDialog.ShowMessageDialog("确定要删除该数据吗？", UILocalize.AskTitle, true, UIStyle.Red, true, false))
            {
                var model = dgvTransportGroup.CurrentRow.DataBoundItem as DirectQuoteModel;
                directQuoteBLL.Delete(model);
                RefreshDgv();
            }
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            var dgv = (UIDataGridView)sender;
            Edit(dgv.CurrentRow.DataBoundItem as DirectQuoteModel);
            RefreshDgv();
        }

        private void Edit(DirectQuoteModel model = null)
        {
            string[] statuNames = new[] { "停用", "启用" };

            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;
            if (model != null)
            {
                option.Text = "分组设置";
                option.AddText("DirectName", "分组名称", model.DirectName, true);
                option.AddCombobox("IsUse", "状态", statuNames, model.IsUse, true, true);
            }
            else
            {
                option.Text = "新增分组";
                option.AddText("DirectName", "分组名称", null, true);
                option.AddCombobox("IsUse", "状态", statuNames);
            }
            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if (model != null)
                {
                    model.DirectName = frm["DirectName"].ToString();
                    model.IsUse = frm["IsUse"].ToString()=="1"?1:0;

                    if (directQuoteBLL.Update(model).IsSuccess)
                    {
                        UIMessageTip.ShowOk("修改成功");
                        LogObj.Info("修改出发链分组" + model.DirectName + "成功;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                    else
                    {
                        UIMessageBox.ShowError("修改失败！");
                        LogObj.Error("修改出发链分组" + model.DirectName + "失败;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                }
                else
                {
                    model = new DirectQuoteModel();
                    model.Type = "CFL";
                    model.DirectName = frm["DirectName"].ToString();
                    model.IsUse = frm["IsUse"].ToString() == "1" ? 1 : 0;

                    if (directQuoteBLL.Add(model).IsSuccess)
                    {
                        UIMessageTip.ShowOk("新增成功");
                        LogObj.Info("新增出发链分组" + model.DirectName + "成功;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                    else
                    {
                        UIMessageBox.ShowError("新增失败！");
                        LogObj.Error("新增出发链分组" + model.DirectName + "失败;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                }
            }
            frm.Dispose();
        }

        private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (e.ColumnIndex == 2)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dgv.Rows[e.RowIndex];
                if (row != null)
                {
                    if (row.Cells[e.ColumnIndex].Value.ToString() == "1")
                    {
                        e.Value = string.Format("{0}",
                          "启用");
                    }
                    else
                    {
                        e.Value = string.Format("{0}",
                      "停用");
                    }
                }
            }
        }
    }
}
